/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.entityGenerator.base.DataTypeGeneratorContext;
import lombok.var;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;

public class AssCloneMethodGenerator extends ClassMethodGenerator {
    private IGspCommonField field;
    private GspAssociation association;
    private DataTypeGeneratorContext dataTypeGenContext;
    private String belongClassType;
    private final String varResult = "result";

    public AssCloneMethodGenerator(IGspCommonField field, GspAssociation association, DataTypeGeneratorContext dataTypeGenContext, String belongClassType) {
        this.field = field;
        this.association = association;
        this.dataTypeGenContext = dataTypeGenContext;
        this.belongClassType = belongClassType;
    }

    @Override
    protected Block buildMethodBody() {
        Block block = ast.newBlock();

//        addMemoCloneStatement(list);
//        addPropertyStatements(list);
//        addReturnStatements(list);
        return block;
    }

//    private void addMemoCloneStatement(java.util.ArrayList<StatementSyntax> list) {
//        list.add(SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(belongClassType)).WithVariables(SyntaxFactory.<VariableDeclaratorSyntax>SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(varResult)).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName("MemberwiseClone"))), SyntaxFactory.IdentifierName(belongClassType))))))));
//    }
//
//    private void addPropertyStatements(java.util.ArrayList<StatementSyntax> list) {
//        for (var item : association.getRefElementCollection()) {
//            if (item.getIsUdt()) {
//                addUdtRefElementStatements(list, item);
//            } else if (item.getObjectType() == GspElementObjectType.Association) {
//                addAssRefElementStatements(list, item);
//            }
//            //其他类型属性，都通过MemberwiseClone自动处理。
//        }
//    }

//    /**
//     * @param list
//     * @param field //if(Address!=null)
//     *              //{
//     *              // result.Address=Address.Copy() as IAddress;
//     *              //}
//     */
//    private void addUdtRefElementStatements(java.util.ArrayList<StatementSyntax> list, IGspCommonField field) {
//        java.util.ArrayList<StatementSyntax> ifBodys = new java.util.ArrayList<StatementSyntax>();
//        addUdtIfBodies(ifBodys, field);
//        list.add(SyntaxFactory.IfStatement(SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, SyntaxFactory.IdentifierName(field.getLabelID()), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), SyntaxFactory.Block(ifBodys.toArray(new StatementSyntax[]{}))));
//    }
//
//    private void addUdtIfBodies(java.util.ArrayList<StatementSyntax> ifBodies, IGspCommonField field) {
//        TypeSyntax typeSyntax = GetUdtRefElementTypeSyntax(field, var propType);
//        if (propType != null && propType.GetMethod("Copy") == null) {
//            return;
//        }
//
//        ifBodies.add(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(varResult), SyntaxFactory.IdentifierName(field.getLabelID())), SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(field.getLabelID()), SyntaxFactory.IdentifierName("Copy"))), typeSyntax))));
//    }
//
//    private TypeSyntax getUdtRefElementTypeSyntax(IGspCommonField field, RefObject<java.lang.Class> type) {
//        return Utils.GetRefElementPropertyTypeSyntax(field, association, dataTypeGenContext.EntityGenContext.CefGenContext, type);
//    }
//
//    private void addAssRefElementStatements(java.util.ArrayList<StatementSyntax> list, IGspCommonField field) {
//        java.util.ArrayList<StatementSyntax> ifBodys = new java.util.ArrayList<StatementSyntax>();
//        AddAssRefElementIfBodies(ifBodys, field);
//        list.add(SyntaxFactory.IfStatement(SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, SyntaxFactory.IdentifierName(field.getLabelID()), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), SyntaxFactory.Block(ifBodys.toArray(new StatementSyntax[]{}))));
//    }
//
//    private void addAssRefElementIfBodies(java.util.ArrayList<StatementSyntax> ifBodies, IGspCommonField field) {
//        TypeSyntax typeSyntax = Utils.getRefElementPropertyTypeSyntax(field, association, dataTypeGenContext.EntityGenContext.CefGenContext, var type);
//        ifBodies.add(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(varResult), SyntaxFactory.IdentifierName(field.getLabelID())), SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(field.getLabelID()), SyntaxFactory.IdentifierName("Clone"))), typeSyntax))));
//    }
//
//    private void addReturnStatements(java.util.ArrayList<StatementSyntax> list) {
//        list.add(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(varResult)));
//    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<Modifier.ModifierKeyword>(java.util.Arrays.asList(new Modifier.ModifierKeyword[]{
                Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    @Override
    protected String getMethodName() {
        return "clone";
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        return new ArrayList<ParameterInfo>();

    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo("Object");
    }
}
